package com.filldream.fastboot.common.entity;

import cn.hutool.core.date.DateUtil;
import com.filldream.fastboot.common.util.RandomUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Date;

/**
 * @author RickSun
 * @version 1.0
 * @data 2020-12-05 11:04
 * @version 2.0 by 2020-09-10
 * @description 登陆错误次数安全验证
 */
@Accessors(chain = true)
@Data
public class LoginEx {

    //---------------固定冻结规定---------------

    /**
     * 超过此数组，则变为IP封禁,单位秒
     * 1、[]-无需验证码
     * 2、[0,0,0,-1,-1] 错误次数验证码，无需冻结
     * 3、[-1] 首次不要，其余一直要验证码
     * 4、[0,0,0,1,2,3] 时间冻结
     * 备注：没有一直需要验证码的功能
     **/
    @ApiModelProperty(value = "限制时间间隔")
//    private int[] limitTime =  new int[] {0,0,0,0,0,30,60,60 * 5,60 * 10,60 *15,60 * 60,60 * 60 * 6,60 * 60 * 12,60 * 60 * 24};
//    private int[] limitTime =  new int[] {0,0,0,0,0,0,0,1,2,3};
    private int[] limitTime =  new int[] {};

    @ApiModelProperty(value = "封禁IP时长 若时长等于0表示关闭IP封禁功能")
//    private Integer ipLimitTime = 60 * 60 * 24 * 7;
    private Integer ipLimitTime = 0;

    private String ip = RandomUtil.randAlphaNumString(20);

    //---------------固定冻结规定---------------

    @ApiModelProperty(value = "当前错误次数")
    private int errCount = 0;

    @ApiModelProperty(value = "上次错误时间")
    private Date lastErrTime;

    @ApiModelProperty(value = "解封时间")
    private Date canLoginTime = new Date();

    @ApiModelProperty(value = "封禁类型 true-普通时长 false-IP时长")
    private Boolean isNormalEx = true;

    @ApiModelProperty(value = "下次是否需要验证码 true-需要 false-不需要")
    private Boolean nextIsNeed = false;

    @ApiModelProperty(value = "本次是否需要验证码 true-需要 false-不需要")
    private Boolean nowIsNeed = false;

    @ApiModelProperty(value = "下次再错是否需要验证吗 true-需要 false-不需要")
    private Boolean nextIsNeedWhenError = false;

    /**
     * @author RickSun
     * @Description 需要验证码的错误次数
     **/
    public static int needCodeCount(int[] timeList){
        for (int i = 0; i < timeList.length; i++) {
            if(timeList[i] != 0){
//                log.info("---->免验次数:" + i);
                return i;
            }
        }
        return 0;
    }

    /**
     * @author RickSun
     * @Description 初始化:下次再错是否需要验证码
     **/
    public void initNextIsNeedWhenError(){
        this.nextIsNeedWhenError =  errCount >= needCodeCount( limitTime );
    }

    /**
     * @author RickSun
     * @Description 初始化: 下次是否需要验证码
     **/
    public void initNextIsNeed(){
        this.nextIsNeed =  errCount > needCodeCount( limitTime );
    }

    public void initNowIsNeed(){
        this.nowIsNeed =  errCount > needCodeCount( limitTime );
    }

    /**
     * @author RickSun
     * @Description 初始化： 封禁类型
     **/
    public void initType(){
        //一直验证码,无需封禁IP
        if(  this.limitTime.length == 1 && this.limitTime[0] == -1 ){
            this.isNormalEx = true;
        }else{
            this.isNormalEx = !(this.errCount > this.limitTime.length -1);
        }
    }

    public void initCanLoginTime(){
        if(  this.limitTime.length == 1 && this.limitTime[0] == -1 ){
            //不做操作
            return;
        }
        if( errCount > limitTime.length -1){
            if( maxLimitTime() == -1 ){
                //不做任何操作
            }else if( ipLimitTime > 0 ){
                this.isNormalEx = false;
                this.canLoginTime = DateUtil.offsetSecond(lastErrTime,ipLimitTime);
            }
        }else{
            this.canLoginTime = DateUtil.offsetSecond(lastErrTime,limitTime[errCount]);
        }
    }

    /**
     * @author RickSun
     * @Description 最大普通动接时间
     **/
    public Integer maxLimitTime(){
        boolean isNeedTime = false;
        if( limitTime.length == 0 ){
            return ipLimitTime;
        }else{
            for (int i : limitTime) {
                if( i >= 0 ){
                    isNeedTime = true;
                }
            }
        }
        if( isNeedTime ){
            return this.limitTime[this.limitTime.length - 1];
        }
        return 0;
    }

    public boolean initExConfig(){
        //无需验证码可一直错
        if( limitTime.length < 1 ){
            this.nextIsNeed = false;
            this.errCount = 0;
            this.nextIsNeedWhenError = false;
            this.nowIsNeed = false;
            return true;
        }
        return false;
    }

}
